package com.sskj.aikeeper.hangqing.fragment;

import android.annotation.SuppressLint;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.presenter.CoinListPresenter;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.ColorEnum;
import com.sskj.lib.util.CommonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.APP_FRAGMENT_COIN_LIST)
public class CoinListFragment extends BaseFragment<CoinListPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @Autowired
    String coinType;
    private BaseAdapter<CoinBean> adapter;
    private List<CoinBean> coins = new ArrayList<>();

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mPresenter != null) {
            if (coinType == null) {
                mPresenter.getHangqingData("");
            } else {
                mPresenter.getHangqingData(coinType);
            }
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_coin_list;
    }

    @Override
    public CoinListPresenter getPresenter() {
        return new CoinListPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        HashMap<String, Integer> iconMap = new HashMap<>();
        iconMap.put("BTC/AICASH", R.mipmap.icon_btc);
        iconMap.put("BTC/USDT", R.mipmap.icon_btc);
        iconMap.put("EOS/AICASH", R.mipmap.icon_eos);
        iconMap.put("EOS/USDT", R.mipmap.icon_eos);
        iconMap.put("ETH/AICASH", R.mipmap.icon_eth);
        iconMap.put("ETH/USDT", R.mipmap.icon_eth);
        iconMap.put("BCH/AICASH", R.mipmap.icon_bch);
        iconMap.put("BCH/USDT", R.mipmap.icon_bch);
        iconMap.put("ETC/AICASH", R.mipmap.icon_etc);
        iconMap.put("ETC/USDT", R.mipmap.icon_etc);


        iconMap.put("LTC/AICASH", R.mipmap.icon_ltc);
        iconMap.put("LTC/USDT", R.mipmap.icon_ltc);
        iconMap.put("AICASH/USDT", R.mipmap.icon_ai);
        iconMap.put("USDT/AICASH", R.mipmap.icon_usdt);

        iconMap.put("XRP/AICASH", R.mipmap.icon_xrp);
        iconMap.put("XRP/USDT", R.mipmap.icon_xrp);

        iconMap.put("ADA/AICASH", R.mipmap.icon_ada);
        iconMap.put("ADA/USDT", R.mipmap.icon_ada);

        iconMap.put("ZEC/AICASH", R.mipmap.icon_zec);
        iconMap.put("ZEC/USDT", R.mipmap.icon_zec);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.commonTextEEE))
        );
        adapter = new BaseAdapter<CoinBean>(R.layout.app_recy_item_coin, null, recyclerView, false) {
            @Override
            public void bind(ViewHolder holder, CoinBean item) {
                holder.setImageResource(R.id.viewCoin, iconMap.get(item.getCode()))
                        .setText(R.id.tvCode, item.getCode().substring(0, item.getCode().indexOf("/")))
                        .setText(R.id.tv_small, item.getCode().substring(item.getCode().indexOf("/")))
                        .setText(R.id.tvUSDT, NumberUtil.keep2(item.getPrice()))
                        .setText(R.id.tvRate, item.getChangeRate())
                        .setTextColor(R.id.tvUSDT, item.isUp() ? ContextCompat.getColor(App.INSTANCE, R.color.libUpColor) : ContextCompat.getColor(App.INSTANCE, R.color.libDownColor))
                        .setBackgroundRes(R.id.tvRate, item.isUp() ? R.drawable.app_shape_corner35_ff5 : R.drawable.app_shape_corner35_03c);
            }
        };
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                ARouter.getInstance().build(RConfig.APP_ACTIVITY_CHART)
                        .withSerializable(Constans.COIN_BEAN, adapter.getData().get(position))
                        .withString(Constans.COIN_CODE, adapter.getData().get(position).getCode())
                        .navigation();
            }
        });
    }

    @Override
    public void initData() {
        if (coinType == null) {
            mPresenter.getHangqingData("");
        } else {
            mPresenter.getHangqingData(coinType);
        }
    }

    @SuppressLint("CheckResult")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshCoin(CoinBean coinBean) {
        if (coinBean == null) {
            return;
        }
        if (coinBean.getCode() == null) {
            return;
        }
        if (adapter == null) {
            return;
        }
        Flowable.fromIterable(adapter.getData())
                .filter(item -> item.getCode().equals(coinBean.getCode()))
                .subscribe(item -> {
                    coinBean.setLight(true);
                    int position = adapter.getData().indexOf(item);
                    adapter.setData(position, coinBean);
                    adapter.notifyItemChanged(position, 1);
                }, e -> e.printStackTrace());
    }


    public void getHangqingDataOK(List<CoinBean> data) {
        if (data == null) {
            return;
        }
        coins.clear();
        Observable.fromIterable(data)
                .filter(coinBean -> !coinBean.getCode().isEmpty())
                .subscribe(new Observer<CoinBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CoinBean coinBean) {
                        coins.add(coinBean);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        adapter.setNewData(coins);
                    }
                });
    }

}
